var $ = getApp();
module.exports = {
	onLoad:function(){
		this.setData({platform : $.platform,mid:$.mid,aid:$.aid,pre_url:$.pre_url,indexurl:$.indexurl,textset:$.textset});
		this._onLoad && this._onLoad();
		this.inittabbar();
	},
	inittabbar:function(){
		var that = this
		if($.tabBarData){
			that.setData({tabBarData : $.tabBarData,indexurl:$.indexurl,textset:$.textset});
			that.settabbar();
		}else{
			$.post('index/gettabbardata',{fromw7:$.config.fromw7},function(res){
				that.setData({tabBarData : res.data,indexurl:res.indexurl,textset:res.textset});
				$.tabBarData = res.data
				$.indexurl = res.indexurl
				$.textset = res.textset
				$.menu2data = res.menu2data
				//$.accounttype = res.accounttype
				//that.setData({accounttype:res.accounttype})
				that.settabbar();
			},false)
		}
	},
	settabbar:function(){
		var that = this
		var thispage = $.getpage();
		//console.log(thispage)
		var currenturl  = '/'+ (thispage.route ? thispage.route : thispage.__route__);
		//console.log(currenturl)
		//console.log('------------------')
		if(thispage.options && thispage.options.id){
			currenturl+='?id='+thispage.options.id
		}else if(thispage.options && thispage.options.gid){
			currenturl+='?gid='+thispage.options.gid
		}else if(thispage.options && thispage.options.bid){
			currenturl+='?bid='+thispage.options.bid
		}
		//console.log(thispage)
		var currentIndex = -1;
		var hastabbar = false;
		var tabBarData = this.data.tabBarData;
		var tablist = tabBarData['list'];
		for(var i=0;i<tablist.length;i++){
			if(tablist[i]['pagePath'] == currenturl){
				currentIndex = i;
				hastabbar = true;
				tabBarData['list'][i].color = tabBarData['selectedColor']
			}else{
				tabBarData['list'][i].color = tabBarData['color']
			}
		}
		$.menuIndex = currentIndex;
		if(hastabbar == false){
			var menu2data = $.menu2data
			if(menu2data.length > 0){
				for(var i in menu2data){
					if(menu2data[i].indexurl == currenturl){
						hastabbar = true;
						currentIndex = 10;
						tabBarData = menu2data[i]
					}
				}
			}
		}
		that.setData({currentIndex:currentIndex,hastabbar:hastabbar,tabBarData:tabBarData})
	},
	goto: function (e) {
		//console.log(e)
		var url = e.currentTarget.dataset.url;
		var opentype = e.currentTarget.dataset.opentype
		$.goto(url,opentype);
	},
	goback: function () {
		wx.navigateBack({fail:function(){
			$.goto($.indexurl)
		}});
	},
	searchgoto: function (e) {
    console.log('search222')
		var url = e.currentTarget.dataset.url;
		if (url.indexOf('?') > 0) {
			url += '&keyword='+e.detail.value;
		}else{
			url += '?keyword='+e.detail.value;
		}
		var opentype = e.currentTarget.dataset.opentype
		$.goto(url,opentype);
	},
	previewImage: function (e) {
		var imgurl = e.currentTarget.dataset.url
		var imgurls = e.currentTarget.dataset.urls
		if (!imgurls) imgurls = imgurl;
		if (typeof (imgurls) == 'string') imgurls = imgurls.split(',');
		wx.previewImage({
			current: imgurl, // 当前显示图片的http链接
			urls: imgurls // 需要预览的图片http链接列表
		})
	},
	phone:function(e) {
		var phone = e.currentTarget.dataset.phone;
		wx.makePhoneCall({
			phoneNumber: phone,
			fail: function () {

			}
		});
	},
	program: function (e) {
		var data = e.currentTarget.dataset.data;
		data = data.split(",");
		wx.navigateToMiniProgram({
			appId: data[0],
			path: data[1],
			envVersion: 'release',
			complete: function() {
				//console.log(data[1]);
			}
		});
	},
	onPullDownRefresh: function () {
		this.onLoad();
		wx.stopPullDownRefresh()
	},
	reLoad: function () {
		this.onLoad();
	},
	onShareAppMessage: function () {
		var thisurl = $._fullurl();
		console.log(thisurl);
		if(this.options.scene){
			var scene = this.options.scene
			var scenes = scene.split('_');
			thisurl += '?scene=' + $.mid + (scenes[1] ? '_' + scenes[1] :'') + (scenes[2] ? '_' + scenes[2] :'');
		}else{
			if (thisurl.indexOf('?') > 0) {
				thisurl += '&scene=' + $.mid
			} else {
				thisurl += '?scene=' + $.mid
			}
		}
		console.log(thisurl);
		var title = '';
		if(this.data.title){
			title = this.data.title;
		}else if(this.data._sysset){
			title = this.data._sysset.name;
		}
		return {
			title: title,
			imageUrl: this.data.sharepic ? this.data.sharepic:'',
			path: thisurl
		}
	},
	onShareTimeline:function(){
		var thisurl = $._fullurl();
		if(this.options.scene){
			var scene = this.options.scene
			var scenes = scene.split('_');
			thisurl += '?scene=' + $.mid + (scenes[1] ? '_' + scenes[1] :'') + (scenes[2] ? '_' + scenes[2] :'');
		}else{
			if (thisurl.indexOf('?') > 0) {
				thisurl += '&scene=' + $.mid
			} else {
				thisurl += '?scene=' + $.mid
			}
		}
		var query = thisurl.split('?')[1];
		var title = '';
		if(this.data.title){
			title = this.data.title;
		}else if(this.data._sysset){
			title = this.data._sysset.name;
		}
		return {
			title: title,
			imageUrl: this.data.sharepic ? this.data.sharepic:'',
			query: query
		}
	},
	menuIndexChange:function(e){
		this.setData({menuIndex:e.detail});
	},
	//授权获取用户信息
	getUserInfo:function(authCanClose){
		//是否可关闭
		if(typeof authCanClose == 'undefined'){
			var authCanClose = true;
		}
		if($.platform == 'wx'){
			this.setData({ needAuth:2,authCanClose:authCanClose})
		}else{
			
		}
	},
	authModalClose:function(){
		this.setData({needAuth:0})		
	},
	//微信授权获取用户信息
	bindGetUserInfo: function(e) {
		var that = this
		var userInfo = e.detail.userInfo
		if(e.detail.userInfo){
			//console.log("点击允许授权");
			$.post('pub/userinfo', { userInfo: userInfo,encryptedData:e.detail.encryptedData,iv: e.detail.iv }, function (res) {
				if(res.status == 1){
					$.mid = res.mid;
					that.setData({needAuth:0});
					that.onLoad();
				}else{
					$.alert(res.msg)
				}
			});
		}else{
			//console.log("用户拒绝授权");
		}
	},
	//手机号登录
	tellogin:function(e){
		var that = this
		var formdata = e.detail.value;
		var tel = formdata.tel;
		var code = formdata.code;
		if(tel==''){
			$.alert('请输入手机号码');
			return false;
		}
		if(!(/^1[3456789]\d{9}$/.test(tel))){
			$.alert("手机号码有误，请重填");
			return false;
		}
		if(code==''){
			$.alert('请输入短信验证码');
			return false;
		}
		$.post('Pub/login',formdata,function(res){
			if(res.status==1){
				$.success(res.msg);
				$.mid = res.mid;
				that.setData({needAuth:0});
				that.onLoad();
			}else{
				$.alert(res.msg);
			}
		},'登录中');
	},
	logintelinput:function(e){
		this.setData({logintel:e.detail.value})
	},
	sendsmscode:function(){
		var that = this
		if(that.data.smshqing==1) return;
		that.setData({smshqing:1});
		var tel = that.data.logintel;
		if(tel==''){
			$.alert('请输入手机号码');
			that.setData({smshqing:0});
			return false;
		}
		if(!(/^1[3456789]\d{9}$/.test(tel))){
			$.alert("手机号码有误，请重填");
			that.setData({smshqing:0});
			return false;
		}
		$.post("pub/sendsms",{tel:tel},function(data){
			if(data.status!=1){
				$.alert(data.msg);
			}
		});
		var time = 120;
		var t = setInterval(function() {
			time--;
			if (time < 0) {
				that.setData({sendsmsdjs:'重新获取'})
				that.setData({smshqing:0});
				clearInterval(t);
			} else if (time >= 0) {
				that.setData({sendsmsdjs:time+'秒'})
			}
		}, 1000);
	},
	uploadimg:function(e){
		var that = this;
		var field= e.currentTarget.dataset.field
		var pics = that.data[field]
		if(!pics) pics = [];
		$.chooseImage(function(urls){
			for(var i=0;i<urls.length;i++){
				pics.push(urls[i]);
			}
			var res = {};
			res[field] = pics
			that.setData(res);
		},1)	
	},
	removeimg:function(e){
		var that = this;
		var index= e.currentTarget.dataset.index
		var field= e.currentTarget.dataset.field
		var pics = that.data[field]
		pics.splice(index,1);
		var res = {};
		res[field] = pics
		console.log(res)
		that.setData(res);
	},
	xuanzhuan:function(e){
		var that = this;
		var index= e.currentTarget.dataset.index
		var field= e.currentTarget.dataset.field
		var pics = that.data[field]
		$.post('imageupload/xuanzhuan',{ url:pics[index]},function(res){
			if(res.status==1){
				pics[index] = res.url
			}else{
				$.error(res.msg);
			}
			var res = {};
			res[field] = pics
			that.setData(res);
		});
	},
	openLocation:function(e){
		//console.log(e)
		var latitude = parseFloat(e.currentTarget.dataset.latitude)
		var longitude = parseFloat(e.currentTarget.dataset.longitude)
		var address = e.currentTarget.dataset.address
		wx.openLocation({
     latitude:latitude,
     longitude:longitude,
		 name:address,
     scale: 13
   })		
	},
	getDistance: function (lat1, lng1, lat2, lng2) {
		if(!lat1 || !lng1 || !lat2 || !lng2) return '';
		var rad1 = lat1 * Math.PI / 180.0;
		var rad2 = lat2 * Math.PI / 180.0;
		var a = rad1 - rad2;
		var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
		var r = 6378137;
		var juli = r * 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(rad2) * Math.pow(Math.sin(b / 2), 2)));
		juli = juli/1000
		juli = juli.toFixed(2);
		return juli;
	}
}